#!/bin/bash
set -e

TARGET_SOC="rk3588"
GCC_COMPILER=aarch64-linux-gnu

export LD_LIBRARY_PATH=${TOOL_CHAIN}/lib64:$LD_LIBRARY_PATH
export CC=${GCC_COMPILER}-gcc
export CXX=${GCC_COMPILER}-g++

ROOT_PWD=$( cd "$( dirname $0 )" && cd -P "$( dirname "$SOURCE" )" && pwd )

echo "ROOT_PWD: ${ROOT_PWD}"

# build
BUILD_DIR=${ROOT_PWD}/build/

# 如果存在上一次的 build 文件夹，先删除
if [[ -d "${BUILD_DIR}" ]]; then
  echo "Removing previous build directory: ${BUILD_DIR}"
  rm -rf ${BUILD_DIR}
fi

# 创建新的 build 文件夹
mkdir -p ${BUILD_DIR}

# 执行 cmake，显式指定输入和输出目录
echo "Running cmake..."
cmake -B ${BUILD_DIR} -S ${ROOT_PWD} -DCMAKE_SYSTEM_NAME=Linux -DTARGET_SOC=${TARGET_SOC}

# 进入 build 文件夹并执行构建
cd ${BUILD_DIR}
echo "Current directory after cd: $(pwd)"

echo "Running make..."
make -j $(nproc)

echo "Running make install..."
make install

echo "Build completed successfully."

cd -
